package com.coldchain.cys.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.cys.constant.Constant;
import com.coldchain.cys.entity.CarDriver;
import com.coldchain.cys.mapping.ApiUtils;
import com.coldchain.cys.page.CarDriverQueryVo;
import com.coldchain.cys.service.ICarDriverService;
import com.fangliai.core.context.BaseContextHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tianpeng
 * @since 2018-08-28
 */
@RestController
@Api(description="承运商系统司机模块")
public class CarDriverController {

	@Autowired
	private ICarDriverService carDriverService ;

	@ApiOperation(value="司机启用禁用开关")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="driverid",value="司机的id",dataType="Long",paramType="query"),
	})
	@CheckUserToken
	@RequestMapping(value=ApiUtils.DO_SWITCH_CAR_DRIVER_USE,method={RequestMethod.POST})
	public BaseResponse doSwitchCarDriverUse(@ApiIgnore Long driverid){
		return carDriverService.doSwitchCarDriverUse(driverid) ;
	}
	 
	@ApiOperation(value="获取当前登录司机")
	@ApiImplicitParams(value={
	})
	@RequestMapping(value=ApiUtils.GET_CURRENT_CAR_DRIVER,method={RequestMethod.GET})
	@CheckUserToken
	public ObjectRestResponse<CarDriver> getCurrentCarDriver(){
		CarDriver carDriver = carDriverService.getCurrent();
		return new ObjectRestResponse<CarDriver>(carDriver) ;
	}
	
	@ApiOperation(value="验证司机登录")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="phone",value="司机的账号",dataType="String",paramType="query"),
		@ApiImplicitParam(name="password",value="司机的密码",dataType="String",paramType="query"),
	})
	@RequestMapping(value=ApiUtils.VALIDATE_CAR_DRIVER,method={RequestMethod.POST})
	public BaseResponse validateCarDriver(@ApiIgnore String username, @ApiIgnore String password){
		return carDriverService.validateCarDriverLogin(username,password) ;
	}
	
	@ApiOperation(value="审核司机")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="driverid",value="司机的Id",dataType="Long",paramType="query"),
		@ApiImplicitParam(name="static",value="审核状态  1审核通过  2审核驳回",dataType="Integer",paramType="query"),
	})
	@RequestMapping(value=ApiUtils.DO_EXAMINE_CAR_DRIVER,method={RequestMethod.POST})
	public BaseResponse doExamineCarDriver(@ApiIgnore Long driverid,@ApiIgnore Integer status){
		return carDriverService.doExamineCarDriver(driverid,status) ;
	}
	
	@ApiOperation(value="取得司机的工作状态标记")
	@CheckUserToken
	@RequestMapping(value=ApiUtils.GET_CAR_DRIVER_WORK_STATUS,method={RequestMethod.GET})
	public ObjectRestResponse<Object> getCarDriverWorkStatus() {
		JSONArray jsonArray = new JSONArray() ;
		JSONObject jsonObject = new JSONObject();
		
		jsonObject.put("id", Constant.CAR_DRIVER_START) ;
		jsonObject.put("desc", "开工") ;
		jsonArray.add(jsonObject) ;
		
		jsonObject = new JSONObject();
		jsonObject.put("id", Constant.CAR_DRIVER_BUSY) ;
		jsonObject.put("desc", "忙碌") ;
		jsonArray.add(jsonObject) ;
		
		jsonObject = new JSONObject();
		jsonObject.put("id", Constant.CAR_DRIVER_OUT) ;
		jsonObject.put("desc", "收工") ;
		jsonArray.add(jsonObject) ;
		
		return new ObjectRestResponse<Object>(jsonArray) ;
	}
	
	@ApiOperation(value="更改当前登录司机的状态")
	@ApiImplicitParams(value={
			@ApiImplicitParam(name="workstatus",value="状态的id（请先获取司机的工作状态标记）",dataType="Integer",paramType="query"),
	})
	@CheckUserToken
	@RequestMapping(value=ApiUtils.DO_CHANGE_CAR_DRIVER_WORK_STATUS,method={RequestMethod.POST})
	public ObjectRestResponse<Object> doChangeCarDriverWorkStatus(Integer workstatus){
		CarDriver carDriver = carDriverService.getCurrent() ;
		if(carDriver == null){
			return new ObjectRestResponse<Object>("无权操作") ;
		}
		carDriver.setWorkstatus(workstatus);
		boolean flag = carDriverService.updateById(carDriver);
		if(!flag){
			return new ObjectRestResponse<Object>("更新失败") ;
		}
		return new ObjectRestResponse<Object>("更新成功") ;
	}
	@RequestMapping(value=ApiUtils.SHOW_CAR_DRIVER_LIST,method={RequestMethod.POST})
	@CheckUserToken
	public Object getDerverList(CarDriverQueryVo vo){
		vo.setCid(BaseContextHandler.getCysID());
		return new ObjectRestResponse<>().data(carDriverService.searchForList(vo)).succeed("ok") ;
	}
	@RequestMapping(value=ApiUtils.SHOW_CAR_DRIVER_PAGE,method={RequestMethod.POST})
	@CheckUserToken
	public Object getDerverForPage(CarDriverQueryVo vo){
		vo.setCid(BaseContextHandler.getCysID());
		return new ObjectRestResponse<>().data(carDriverService.search(vo)).succeed("ok") ;
	}

	/**
	 * 添加或修改司机
	 * @author TianPeng
	 *
	 * 2018年9月14日
	 */
	@ApiIgnore
	@RequestMapping(value=ApiUtils.DO_EDIT_CAR_DRIVER,method={RequestMethod.POST})
	@CheckUserToken
	public BaseResponse edit(CarDriver carDriver) throws IOException{
		return carDriverService.edit(carDriver) ;
	}
	
	@ApiOperation(value="司机 id 查询 你懂我意思吧")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="id",value="司机的id",dataType="Long",paramType="path"),
	})
	@RequestMapping(value=ApiUtils.GET_CAR_DRIVER_INFO + "/{id}",method={RequestMethod.GET})
	@CheckUserToken
	public ObjectRestResponse<CarDriver> getCarDriverInfo(@ApiIgnore @PathVariable Long id){
		CarDriver carDriver = carDriverService.getCarDriverInfo(id) ;
		if(carDriver == null){
			return (ObjectRestResponse<CarDriver>) new ObjectRestResponse<CarDriver>().error("无权操作") ;
		}
		return new ObjectRestResponse<CarDriver>(carDriver) ;
	}
	
	/**
	 * 删除司机
	 * @author TianPeng
	 *
	 * 2018年9月14日
	 */
	@ApiIgnore
	@RequestMapping(value=ApiUtils.DO_DELETE_CAR_DRIVER + "/{id}",method={RequestMethod.POST})
	@CheckUserToken
	public BaseResponse doDeleteCarDriver(@PathVariable Long id){
		CarDriver carDriver = carDriverService.getCarDriverInfo(id) ;
		if(carDriver == null){
			return new ObjectRestResponse<>().error("无权操作") ;
		}
		boolean result = carDriverService.removeById(id);
		if(result) {
			return new ObjectRestResponse<>().succeed("删除成功") ;
		}else {
			return new ObjectRestResponse<>().error("删除异常") ;
		}
	}

	/**
	 * 查询没有绑定车辆的司机
	 * @param search 模糊查询 电话 or 名字
	 * @return
	 */
	@ApiIgnore
	@CheckUserToken
	@RequestMapping(value=ApiUtils.GET_NOT_CAR_ON_DRIVER,method= {RequestMethod.GET})
	public ObjectRestResponse<List<CarDriver>> getNotCarOnDriver(String search) {
		List<CarDriver> carDrivers = carDriverService.getNotCarList(search) ;
		return new ObjectRestResponse<List<CarDriver>>(carDrivers) ;
	}
	
	@ApiOperation(value="更改司机的个人信息")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="name",value="司机的昵称",dataType="String",paramType="query"),
		@ApiImplicitParam(name="headImage",value="司机的头像",dataType="String",paramType="query"),
	})
	@CheckUserToken
	@RequestMapping(value=ApiUtils.DO_SAVE_CAR_DRIVER_INFO,method= {RequestMethod.POST})
	public BaseResponse doSaveCarDriverInfo(@ApiIgnore String name,@ApiIgnore String headImage) {
		return carDriverService.doSaveCarDriverInfo(name,headImage) ;
	}
	
	@ApiOperation(value="司机自动接单开关")
	@RequestMapping(value=ApiUtils.DO_SWITCH_CAR_DRIVER_AUTO_RECEIVE,method= {RequestMethod.POST})
	@CheckUserToken
	public BaseResponse doSwitchCarDriverAutoReceive() {
		return carDriverService.doSwitchCarDriverAutoReceive() ;
	}

	@ApiOperation(value="司机语音播报开关")
	@CheckUserToken
	@RequestMapping(value=ApiUtils.DO_SWITCH_CAR_DRIVER_VOICE_NOTICE,method= {RequestMethod.POST})
	public BaseResponse doSwitchCarDriverVoiceNotice() {
		return carDriverService.doSwitchCarDriverVoiceNotice() ;
	}
	
	@ApiOperation(value="司机接单时间设置")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="worktimeBegin",value="接单开始时间（例：09:00）",dataType="String",paramType="query"),
		@ApiImplicitParam(name="worktimeEnd",value="接单结束时间（例：18:30）",dataType="String",paramType="query"),
	})
	@CheckUserToken
	@RequestMapping(value=ApiUtils.DO_SET_RECEIVE_TIME,method= {RequestMethod.POST})
	public BaseResponse doSetReceiveTime(@ApiIgnore String worktimeBegin,@ApiIgnore String worktimeEnd) {
		return carDriverService.doSetReceiveTime(worktimeBegin,worktimeEnd) ;
	}
	
	@ApiOperation(value="司机成功订单数 + 1")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="driverid",value="司机的id",dataType="Long",paramType="query"),
	})
	@CheckUserToken
	@RequestMapping(value=ApiUtils.DO_INCREMENT_SUCCESS_ORDER,method= {RequestMethod.POST})
	public BaseResponse doIncrementSuccessOrder(Long driverid) {
		return carDriverService.doIncrementSuccessOrder(driverid) ;
	}
	
	@ApiOperation(value="司机退单数 + 1")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="driverid",value="司机的id",dataType="Long",paramType="query"),
	})
	@CheckUserToken
	@RequestMapping(value=ApiUtils.DO_INCREMENT_RETREAT_ORDER,method= {RequestMethod.POST})
	public BaseResponse doIncrementRetreatOrder(Long driverid) {
		return carDriverService.doIncrementRetreatOrder(driverid) ;
	}
	
	@ApiOperation(value="发送登录验证码")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="phone",value="司机的手机号码",dataType="String",paramType="query"),
	})
	@RequestMapping(value=ApiUtils.DO_SEND_LOGIN_VALIDATE,method= {RequestMethod.POST})
	public BaseResponse doSendLoginValidate(@ApiIgnore String phone) {
		return carDriverService.doSendLoginValidate(phone) ;
	}
	
	@ApiOperation(value="短信验证码验证登录")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="phone",value="司机的手机号码",dataType="String",paramType="query"),
		@ApiImplicitParam(name="code",value="短信验证码",dataType="String",paramType="query"),
	})
	@RequestMapping(value=ApiUtils.VALIDATE_CAR_DRIVER_CODE,method= {RequestMethod.POST})
	public BaseResponse validateCarDriverCode(@ApiIgnore String phone,@ApiIgnore String code) {
		return carDriverService.validateCarDriverCodeLogin(phone,code) ;
	}
	
	@ApiOperation(value="司机忘记密码发送短信")
	@ApiImplicitParams(value={
		@ApiImplicitParam(name="phone",value="司机的手机号码",dataType="String",paramType="query"),
	})
	@RequestMapping(value=ApiUtils.DO_SEND_FORGET_VALIDATE,method= {RequestMethod.POST})
	public BaseResponse doSendForgetValidate(@ApiIgnore String phone) {
		return carDriverService.doSendForgetValidate(phone) ;
	}
	 
	@ApiOperation(value="验证司机忘记密码")
	@ApiImplicitParams(value={
			@ApiImplicitParam(name="phone",value="司机的手机号码",dataType="String",paramType="query"),
			@ApiImplicitParam(name="code",value="短信验证码",dataType="String",paramType="query"),
	})
	@RequestMapping(value=ApiUtils.VALIDATE_CAR_DRIVER_FORGET,method= {RequestMethod.POST})
	public BaseResponse validateCarDriverForget(@ApiIgnore String phone,@ApiIgnore String code) {
		return carDriverService.validateCarDriverForget(phone,code) ;
	}
	
	
	@ApiOperation(value="司机忘记密码操作设置新密码")
	@ApiImplicitParams(value={
			@ApiImplicitParam(name="token",value="短信验证码的token",dataType="String",paramType="query"),
			@ApiImplicitParam(name="newPass",value="新密码",dataType="String",paramType="query"),
	})
	@RequestMapping(value=ApiUtils.DO_DRIVER_CHANGE_FORGET_PASS,method= {RequestMethod.POST})
	public BaseResponse doDriverChangeForgetPass(@ApiIgnore String token,@ApiIgnore String newPass) {
		return carDriverService.doDriverChangeForgetPass(token,newPass) ;
	}
}

